// #include "lv_drivers/display/drm.h"
#include "lv_demos/lv_demo.h"
#include "lv_demos/src/lv_demo.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/display/monitor.h"
#include "lv_drivers/indev/evdev.h"
#include "lv_drivers/indev/ubuntu_mouse.h"
#include "lv_lib_png/lv_png.h"
#include "lvgl/lvgl.h"
#include "main_page.h"
#include "pthread.h"
#include "unistd.h"
#include <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#define GET_TICK_COUNT() getTime()

lv_obj_t *home_main;
lv_indev_drv_t indev_drv;

long getTime(void) {
    struct timeval time;
    /* 获取时间，理论到us */
    gettimeofday(&time, NULL);
    return (time.tv_sec * 1000 * 1000 + time.tv_usec);
}

int main(int argc, char *argv[]) {
    /*LittlevGL init*/
    lv_init();

#if USE_DRM
    drm_init();
#endif

#if USE_MONITOR
    monitor_init();
#endif

    /*A small buffer for LittlevGL to draw the screen's content*/
    lv_color_t *buf = (lv_color_t *)malloc(LV_HOR_RES_MAX * LV_VER_RES_MAX * sizeof(lv_color_t) * 10);
    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, LV_HOR_RES_MAX * LV_VER_RES_MAX * 10);
    /*Initialize and register a display driver*/
    lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf = &disp_buf;
    /*Initialize and register a display driver*/
    disp_drv.hor_res = LV_HOR_RES_MAX;
    disp_drv.ver_res = LV_VER_RES_MAX;

#if USE_DRM
    disp_drv.flush_cb = drm_flush;
#endif

#if USE_MONITOR
    disp_drv.flush_cb = monitor_flush;
#endif

    lv_disp_drv_register(&disp_drv);

#if USE_DRM
    evdev_init();
#endif

#if USE_MONITOR
    ms_init();
#endif

    lv_indev_drv_init(&indev_drv); /*Basic initialization*/
    indev_drv.type = LV_INDEV_TYPE_POINTER;

#if USE_DRM
    indev_drv.read_cb = evdev_read;
#endif

#if USE_MONITOR
    indev_drv.read_cb = ms_read;
#endif

    lv_indev_drv_register(&indev_drv);

    // png库初始化
    lv_png_init();
    home_main = lv_scr_act();

#if USE_MONITOR
// img_obj = lv_img_create(home_main, NULL);
// lv_img_set_src(img_obj, "./zhizhen.png");
#endif

    // lv_example_tabview_1();
    lv_demo_widgets();

    while (1) {
        usleep(1000);
        lv_task_handler();
    }
    return 0;
}

uint32_t custom_tick_get(void) {
    static uint64_t start_ms = 0;
    static int ret = 0;
    if (start_ms == 0) {
        struct timespec res;
        do {
            ret = clock_gettime(CLOCK_MONOTONIC, &res);
        } while (ret);
        start_ms = (res.tv_sec * 1000000000 + res.tv_nsec) / 1000000;
    }

    uint64_t now_ms;
    struct timespec res_now;
    do {
        ret = clock_gettime(CLOCK_MONOTONIC, &res_now);
    } while (ret);
    now_ms = (res_now.tv_sec * 1000000000 + res_now.tv_nsec) / 1000000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}
